import {ModalForm, ProFormText, ProFormSelect, ProFormMoney, ProFormTextArea} from "@ant-design/pro-components";
import {GoodsVO} from "@/services/GoodsService";
import {useRef, useState} from "react";
import {ProFormInstance} from "@ant-design/pro-form/lib";
import {useModel} from "@@/exports";
import {RequestOptionsType} from "@ant-design/pro-utils";


export interface GoodsUpdateModalProps {
  onFinish: (formData: any) => Promise<boolean | void>;
  goods?: GoodsVO;
  open: boolean;
  onOpenChange: (state: boolean) => void;
}

const GoodsUpdateModal = (props: GoodsUpdateModalProps) => {
  let {getDictList} = useModel("dict");

  const formRef = useRef<ProFormInstance>()
  const [inst, setInst] = useState<GoodsVO | undefined>()

  const onOpenChange = (state: boolean) => {
    if (state) {
      let dto = JSON.parse(JSON.stringify(props.goods || {price: 0}))
      dto.price = dto?.price / 100
      setInst(dto)
      formRef?.current?.setFieldsValue(dto)
    } else {
      setInst(undefined)
      formRef?.current?.resetFields()
    }
    props.onOpenChange(state);
  }
  return <>
    <ModalForm
      formRef={formRef}
      title={props.goods ? "修改套系" : "添加套系"}
      open={props.open}
      initialValues={inst}
      onFinish={props.onFinish}
      onOpenChange={onOpenChange}
    >
      <ProFormText
        label="id"
        hidden
        name="id"
      />


      <ProFormSelect
        label="套系类型"
        name="goodsType"
        rules={[{required: true, message: "请选择套系类型"}]}
        request={async () => getDictList('goods_type') as RequestOptionsType[]}
      />


      <ProFormText
        label="商品名称"
        name="goodsName"
        rules={[{required: true, message: "请填写套系名称"}]}
      />

      <ProFormMoney
        label="价格"
        name="price"
        rules={[{required: true, message: "请填写套系价格"}]}

      />

      <ProFormTextArea
        label="商品描述"
        name="goodsDesc"
      />

    </ModalForm>
  </>
}

export default GoodsUpdateModal
